package com.dailyblue.project.estate.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.House;
import com.dailyblue.project.estate.commons.bean.Letter;
import com.dailyblue.project.estate.commons.handler.HouseException;
import com.dailyblue.project.estate.commons.mapper.HouseMapper;
import com.dailyblue.project.estate.commons.mapper.LetterMapper;
import com.dailyblue.project.estate.commons.mapper.OwnerMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.house.service.HouseService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {
    @Resource
    private HouseMapper houseMapper;

    @Override
    public JsonResult addHouse(House house) {
        boolean b = save(house);
        if (!b) {
            throw new HouseException("添加失败");
        }
        return ResultTool.success("success");
    }

    @Cacheable(cacheNames = "findAllHouses")
    public JsonResult findAllHouses() {
        List<House> list = list();
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findOneHouse(String houseId) {
        House house = getById(houseId);
        return ResultTool.success(house);
    }

    @CacheEvict(cacheNames = "findAllHouses", allEntries = true)
    public JsonResult deleteHouse(String houseId) {
        boolean b = removeById(houseId);
        if (!b) {
            throw new HouseException("删除失败");
        }
        return ResultTool.success("success");
    }

    @CacheEvict(cacheNames = "findAllHouses", allEntries = true)
    public JsonResult updateHouse(House house) {
        UpdateWrapper<House> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", house.getId());
        updateWrapper.set("community_name", house.getCommunityName());
        updateWrapper.set("community_id", house.getCommunityId());
        updateWrapper.set("building_name", house.getBuildingName());
        updateWrapper.set("building_id", house.getBuildingId());
        updateWrapper.set("code", house.getCode());
        updateWrapper.set("name", house.getName());
        updateWrapper.set("owner_id", house.getOwnerId());
        updateWrapper.set("owner_name", house.getOwnerName());
        updateWrapper.set("telephone", house.getTelephone());
        updateWrapper.set("room_num", house.getRoomNum());
        updateWrapper.set("unit", house.getUnit());
        updateWrapper.set("floor", house.getFloor());
        updateWrapper.set("description", house.getDescription());
        updateWrapper.set("live_time", house.getLiveTime());
        int i = houseMapper.update(updateWrapper);
        if (i <= 0) {
            return ResultTool.fail(201, "更新失败");
        }
        return ResultTool.success("修改成功");
    }
}
